
#define GPH2CON		(*(volatile unsigned long *)0xE0200C40)
#define GPH2DAT	(*(volatile unsigned long *)0xE0200C44)
#define GPD0CON  	(*(volatile unsigned long *)0xE02000A0)
#define GPD0DAT	        (*(volatile unsigned long *)0xE02000A4)
// clock
#define APLL_LOCK 	(*(volatile unsigned long *)0xE0100000)	
#define MPLL_LOCK 	(*(volatile unsigned long *)0xE0100008)
#define APLL_CON0 	(*(volatile unsigned long *)0xE0100100)
#define APLL_CON1 	(*(volatile unsigned long *)0xE0100104)
#define MPLL_CON 	(*(volatile unsigned long *)0xE0100108)
#define CLK_SRC0 	(*(volatile unsigned long *)0xE0100200)
#define CLK_SRC1 	(*(volatile unsigned long *)0xE0100204)
#define CLK_SRC2 	(*(volatile unsigned long *)0xE0100208)
#define CLK_SRC3 	(*(volatile unsigned long *)0xE010020c)
#define CLK_SRC4 	(*(volatile unsigned long *)0xE0100210)
#define CLK_SRC5 	(*(volatile unsigned long *)0xE0100214)
#define CLK_SRC6 	(*(volatile unsigned long *)0xE0100218)
#define CLK_SRC_MASK0   (*(volatile unsigned long *)0xE0100280)
#define CLK_SRC_MASK1   (*(volatile unsigned long *)0xE0100284)
#define CLK_DIV0 	(*(volatile unsigned long *)0xE0100300)
#define CLK_DIV1 	(*(volatile unsigned long *)0xE0100304)
#define CLK_DIV2 	(*(volatile unsigned long *)0xE0100308)
#define CLK_DIV3 	(*(volatile unsigned long *)0xE010030c)
#define CLK_DIV4 	(*(volatile unsigned long *)0xE0100310)
#define CLK_DIV5 	(*(volatile unsigned long *)0xE0100314)
#define CLK_DIV6 	(*(volatile unsigned long *)0xE0100318)
#define CLK_DIV7 	(*(volatile unsigned long *)0xE010031c)
#define CLK_DIV0_MASK   0x7fffffff
#define APLL_MDIV       0x7d
#define APLL_PDIV       0x3
#define APLL_SDIV       0x1
#define MPLL_MDIV	0x29b
#define MPLL_PDIV	0xc
#define MPLL_SDIV	0x1
#define set_pll(mdiv, pdiv, sdiv)  (1<<31 | mdiv<<16 | pdiv<<8 | sdiv)
#define APLL_VAL        set_pll(APLL_MDIV,APLL_PDIV,APLL_SDIV)
#define MPLL_VAL	set_pll(MPLL_MDIV,MPLL_PDIV,MPLL_SDIV)
// PWM CLOCK
#define	TCFG0    	( *(volatile unsigned long *)0xE2500000)
#define	TCFG1    	( *(volatile unsigned long *)0xE2500004)
#define	TCON      	( *(volatile unsigned long *)0xE2500008)
#define	TCNTB0    	( *(volatile unsigned long *)0xE250000C)
#define	TCMPB0    	( *(volatile unsigned long *)0xE2500010)
#define	TCNTO0    	( *(volatile unsigned long *)0xE2500014)
#define	TCNTB1    	( *(volatile unsigned long *)0xE2500018)
#define	TCMPB1    	( *(volatile unsigned long *)0xE250001C)
#define	TCNTO1    	( *(volatile unsigned long *)0xE2500020)
#define	TCNTB2    	( *(volatile unsigned long *)0xE2500024)
#define	TCMPB2    	( *(volatile unsigned long *)0xE2500028)
#define	TCNTO2    	( *(volatile unsigned long *)0xE250002C)
#define	TCNTB3    	( *(volatile unsigned long *)0xE2500030)
#define	TCMPB3    	( *(volatile unsigned long *)0xE2500034)
#define	TCNTO3    	( *(volatile unsigned long *)0xE2500038)
#define	TCNTB4    	( *(volatile unsigned long *)0xE250003C)
#define	TCNTO4    	( *(volatile unsigned long *)0xE2500040)
#define	TINT_CSTAT 	( *(volatile unsigned long *)0xE2500044)
void init_clock(void)
{

    	CLK_SRC0 = 0x0;// close PLL	
    	APLL_LOCK= 0x0000FFFF; 
    	MPLL_LOCK = 0x0000FFFF;    	
    	// set PLL  	
    	APLL_CON0 =APLL_VAL;//FOUT= MDIV * FIN / (PDIV*2^(SDIV-1)) = 1000 MHz	
    	MPLL_CON = MPLL_VAL;// FOUT = MDIV*FIN/(PDIV*2^SDIV)=667 MHz		
    	CLK_SRC0= 0x10001111;// FOUT = MDIV*FIN/(PDIV*2^SDIV)=667 MHz	
}
void init_timer(unsigned long tcntb,unsigned long tcmpb)
{
	TCON = 0;
	TCFG0= (TCFG0 & ~0xff00ff)|0x41;//设置预分频系数65
	TCFG1 =(TCFG1 & ~(0xf<<0))|0x04;// 16分频  输入时钟= PCLK /(prescaler+ 1) /divider =62500hz
	TCNTB0 = tcntb;
	TCMPB0 = tcmpb;
	TCON |= 1<<1;// 更新TCNTB0 TCMPB0 
	TCON &= ~(1<<1);// 清手动更新位
	TCON |= (1<<0)|(1<<3);// 选择自动加载并启动timer0 
	//TINT_CSTAT |= 1;
}
static void Delay(long count)
{
    	volatile int i, j = 0;
    	volatile static int loop = 800000000/10000;
    	for(;count > 0;count--)
        	for(i=0;i < loop; i++) { j++; }
}
void main(void)
{

    GPD0CON = (GPD0CON & ~(0xF << 0))|0x2;
    GPH2CON= (GPH2CON & ~0x0000FFFF) | 0x00001111; 
    GPH2DAT|= 0x0F;
    init_clock();//初始化时钟
    init_timer(62500,40000);//初始wttimer                                   
    while(1)
    {
    }
    //TCON = 0;

}
